package com.hiwan.system.web.parameter;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.utils.DateUtils;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.pojo.DictEntry;
import com.hiwan.system.pojo.ParameterInfo;
import com.hiwan.system.service.parameter.ParameterService;
import com.hiwan.system.util.DictCacheUtils;
import com.hiwan.system.util.UserCacheUtils;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/parameter")
public class ParameterController extends BaseController{
	
	@Autowired
	private ParameterService parameterService;

	// 查询大类列表，进入首页
	@RequestMapping("/list.do")
	public String selectMainParamList(ModelMap model) {
		List<DictEntry> dictEntryList =DictCacheUtils.getDictList("D00021");
		model.put("dictEntryList", dictEntryList);
		return "parameter/list";
	}

	@RequestMapping("/subs.do")
	public String selectSubParamList(String paramType, ModelMap model) {
		List<ParameterInfo> subParamList = parameterService.selectSubParamList(paramType);
		model.put("subParamList", subParamList);
		model.put("paramType", paramType);
		return "parameter/subs";
	}

	@RequestMapping("/detail.do")
	public String selectParamInfo(String paramType, String orgId,
			String paramKey, ModelMap model) {
		ParameterInfo paramInfo = parameterService.selectParamInfo(
				paramType, orgId, paramKey);
		model.put("paramInfo", paramInfo);
		model.put("columnNames",parameterService.getParamColumnNames(paramType));
		model.put("modifyFlag", parameterService.getModifyFlag(orgId));
		model.put("operFlag", "update");
		return "parameter/detail";
	}

	@RequestMapping("/update.do")
	public @ResponseBody String[] updateParamInfo(ParameterInfo paramInfo) {
		paramInfo.setUpdateTime(DateUtils.getDateTime());
		paramInfo.setUpdateUser(UserCacheUtils.getId());
		int flag = parameterService.updateParamInfo(paramInfo);
		String[] ret;
		if(flag==1){
			ret = new String[] { "ok", "修改成功！" };
		}else{
			ret = new String[] { "ok", "修改失败！" };
		}
		return ret;
	}

	@RequestMapping("/toAdd.do")
	public String toAdd(String paramType, ModelMap model) {
		ParameterInfo paramInfo = new ParameterInfo();
		paramInfo.setParamType(paramType);
		model.put("paramInfo", paramInfo);
		model.put("columnNames",parameterService.getParamColumnNames(paramType));
		model.put("operFlag", "add");
		return "parameter/detail";
	}

	@RequestMapping("/add.do")
	public @ResponseBody String[] add(ParameterInfo paramInfo) {
		paramInfo.setCreateTime(DateUtils.getDateTime());
		paramInfo.setCreateUser(UserCacheUtils.getId());
		int flag = parameterService.insertParamInfo(paramInfo);
		String[] ret;
		if(flag==1){
			ret = new String[] { "ok", "新增成功！" };
		}else{
			ret = new String[] { "fail", "新增失败！存在相同的参数值，请重新填写" };
		}
		return ret;
	}
	
	@RequestMapping("/delete.do")
	public @ResponseBody String[] delete(String paramType,String orgId,String paramKey) {
		parameterService.deleteParamInfo(paramType,orgId , paramKey);
		return new String[] { "ok", "删除成功！" };	
	}
	@RequestMapping("mobile/selectKhwh")
	public void selectKhwh(HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");  
	    response.setContentType("application/json; charset=utf-8"); 
		Map<String, Object> map = new HashMap<String,Object>();
		PrintWriter out=null;
		try {
			out = response.getWriter();
			ParameterInfo s=new ParameterInfo();
			s.setParamType("khwh");
			List<ParameterInfo> list=parameterService.selectKhwh(s);
			map.put("data", list);
		} catch (Exception e) {
			map.put("msg", "发生错误");
			e.printStackTrace();
		}finally{
			out.append(JSONObject.fromObject(map).toString());
		}
	}
}
